//
//  BookPlaceDetailTravelCell.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/28.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookPlaceDetailTravelCell.h"
#import "PhotoCollectionViewCell.h"
@interface BookPlaceDetailTravelCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    CGFloat x;
    CGFloat cellWidth;
    CGFloat y;
}
@property (nonatomic, strong) UILabel *contents;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *photoArray;

@end

static NSString *cellIdetifier = @"photo";
@implementation BookPlaceDetailTravelCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        x = 20 * KScreenWidth / 414;
        cellWidth = KScreenWidth - 2 * x;
        y = 5 * KScreenWidth / 414;
        self.contents = [[UILabel alloc]initWithFrame:CGRectMake(x, y, cellWidth, 0)];
        self.contents.textColor = [UIColor grayColor];
        self.contents.numberOfLines = 0;
//        self.contents.backgroundColor = [UIColor redColor];
        self.contents.font = [UIFont systemFontOfSize:17 * KScreenWidth / 414];
        [self.contentView addSubview:self.contents];
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 2 * KScreenWidth / 414;
        layout.minimumLineSpacing =2 * KScreenWidth / 414;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(x, self.contents.height, cellWidth, 0) collectionViewLayout:layout];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:cellIdetifier];
        [self.contentView addSubview:self.collectionView];
        
    }
    return self;
}

- (void)setModel:(BookPlaceTravelModel *)model
{
    self.contents.height = 0;
    self.contents.text = @"";
    if (model.comment != nil)
    {
        // 创建实例
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:model.comment];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        // 设置行间距
        [style setLineSpacing:6 * KScreenWidth / 414];
        self.contents.numberOfLines = 0;
        // range 适用于哪一段字体
        [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, model.comment.length)];
        
        CGRect bounds = [model.comment boundingRectWithSize:CGSizeMake(cellWidth, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.contents.font,NSParagraphStyleAttributeName:style} context:nil];
        //    NSLog(@"%@", rowModel.myDescription);
        //    NSLog(@"%@", attStr);
        self.contents.attributedText = attStr;
        self.contents.frame = CGRectMake(x, y, cellWidth, bounds.size.height + 5 * KScreenWidth / 414);
    }
    
    self.photoArray = model.nodesArray;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.frame = CGRectMake(x, self.contents.height + 20 * KScreenWidth / 414, cellWidth, 120 * KScreenWidth / 414);
    [self.collectionView reloadData];
  
    
}

+ (CGFloat)heightForCell:(BookPlaceTravelModel *)model
{
    CGFloat contentHeight = 0;
    if (model.comment != nil)
    {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        style.lineSpacing = 6 * KScreenWidth / 414;
        contentHeight = [model.comment boundingRectWithSize:CGSizeMake(KScreenWidth - 40 * KScreenWidth / 414, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16 * KScreenWidth / 414],NSParagraphStyleAttributeName:style} context:nil].size.height + 5 * KScreenWidth / 414;
    }

    
    return contentHeight + 120 * KScreenWidth / 414 + 40 * KScreenWidth / 414;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.photoArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosModel *model = self.photoArray[indexPath.item];
    if (model.image_url != nil)
    {
        CGFloat height = 120 * KScreenWidth / 414;
        CGFloat wigth = height * model.image_width / model.image_height;
        //    NSLog(@"www == %f", wigth);
        //    NSLog(@"%@", model);
         return CGSizeMake(wigth, height);
    }
    return CGSizeMake(0, 0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdetifier forIndexPath:indexPath];
    PhotosModel *model = self.photoArray[indexPath.row];
    if (model.image_url != nil)
    {
        cell.model = model;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosModel *model = self.photoArray[indexPath.item];
    NSInteger a = 0;
    for (int i = 0; i < self.photoArray.count; i++)
    {
        if ([model isEqual:self.photoArray[i]])
        {
            a = i;
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(JumpPageWithArray:item:)])
    {
        [self.delegate JumpPageWithArray:self.photoArray item:a];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
